﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class QualitySheetItemsLogic : CommonLogic<QualitySheetItems, int>, IQualitySheetItemsLogic
    {
        private IStudentEvalsLogic _seLogic;
        public QualitySheetItemsLogic(IStudentEvalsLogic seLogic)
        {
            _seLogic = seLogic;
        }

        #region Implementation of ICommonLogic<QualitySheetItems, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(QualitySheetItems entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(QualitySheetItems entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("QualitySheetItems.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("QualitySheetItems.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public QualitySheetItems FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<QualitySheetItems>("QualitySheetItems.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("QualitySheetItems.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<QualitySheetItems> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<QualitySheetItems>("QualitySheetItems.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<QualitySheetItems> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        #region Implementation of IQualitySheetItemsLogic

        /// <summary>
        /// 自定义插入
        /// </summary>
        /// <param name="items"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int InsertQualitySheetItems(QualitySheetItems items, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("QualitySheetItems", items);
            ht.Add("SchoolMark", schoolMark);
            return (int)InsertCustomer("QualitySheetItems.InsertQualitySheetItems", ht);
        }

        /// <summary>
        /// 自定义更新
        /// </summary>
        /// <param name="items"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int UpdateQualitySheetItems(QualitySheetItems items, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("QualitySheetItems", items);
            ht.Add("SchoolMark", schoolMark);
            return UpdateCustomer("QualitySheetItems.UpdateQualitySheetItems", ht);
        }

        /// <summary>
        /// 获取QualitySheetItems
        /// </summary>
        /// <param name="studentEvalId"></param>
        /// <param name="destId"></param>
        /// <param name="authorId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public QualitySheetItems GetQualitySheetItemsByCondition(int studentEvalId, int destId, int authorId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("StudentEvalID", studentEvalId);
            ht.Add("DestID", destId);
            ht.Add("AuthorID", authorId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<QualitySheetItems>("QualitySheetItems.GetQualitySheetItemsByCondition", ht);
        }

        /// <summary>
        /// 获取学校内某年级下的班级
        /// </summary>
        public IList<ClsInfo> GetClassesInGrade(String schoolMark, int grade)
        {
            Hashtable ht = new Hashtable();
            ht.Add("Grade", grade);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<ClsInfo>("QualitySheetItems.GetClassesInGrade", ht);
        }

        /// <summary>
        /// 查询个人分析报表
        /// </summary>
        public IList<StudentReportView> GetQualityStudentReport(String schoolMark, int seid, int grade, int cls)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SEID", seid);
            if(cls > 0)
                ht.Add("ClsID", cls);
            if(grade > 0)
                ht.Add("Grade", grade);
            return FindCustomer<StudentReportView>("QualitySheetItems.GetQualityStudentReport", ht);
        }

        /// <summary>
        /// 查询个人分析报表
        /// </summary>
        public IList<ClassReportView> GetQualityClassReport(String schoolMark, int seid, int grade, int cls)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SEID", seid);
            ht.Add("ClsID", cls);
            ht.Add("Grade", grade);
            return FindCustomer<ClassReportView>("QualitySheetItems.GetQualityClassReport", ht);
        }
        #endregion

        
        /// <summary>
        /// 查询是否评完
        /// </summary>
        /// <param name="studentID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool CheckAllState(int studentID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("StudentID", studentID);
            ht.Add("SchoolMark", schoolMark);
            IList<int> idList = FindCustomer<int>("QualitySheetItems.FindStudentEvalIDList", ht);
            IList<StudentEvals> seList = _seLogic.GetStudentEvalByStudentID(studentID, schoolMark);
            IList<int> seIDList = new List<int>();
            foreach (StudentEvals evals in seList)
            {
                seIDList.Add(evals.RID);
            }

            // 判断两个数组是否相等Equals???

            int[] array1 = new int[idList.Count];
            idList.CopyTo(array1, 0);
            int[] array2 = new int[seIDList.Count];
            seIDList.CopyTo(array2, 0);
            return IsEquals(array1, array2);
        }

        /// <summary> 
        /// 比较两个值型一维数组变量是否值相等 
        /// </summary> 
        /// <param name="array1">值型一维数组1</param> 
        /// <param name="array2">值型一维数组2</param> 
        /// <returns>比较结果，相等则true，否则false</returns> 
        private static bool IsEquals(Array array1, Array array2)
        {
            //比较类型是否一样 
            if (!Object.ReferenceEquals(array1.GetType(), array2.GetType()))
            {
                return false;
            }

            //比较长度是否一样 
            if (array1.GetLength(0) != array2.GetLength(0))
            {
                return false;
            }
            //比较成员是否对应相等 
            ValueType v1, v2;
            for (int i = 0; i < array1.GetLength(0); i++)
            {
                v1 = (ValueType)array1.GetValue(i);
                v2 = (ValueType)array2.GetValue(i);
                if (!v1.Equals(v2))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 查询年级分析报表
        /// </summary>
        public IList<GradeReportView> GetQualityGradeReport(String schoolMark, int seid, int grade)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SEID", seid);
            ht.Add("Grade", grade);
            return FindCustomer<GradeReportView>("QualitySheetItems.GetQualityGradeReport", ht);
        }
    }
}
